package com.testing.sunnycommon.controller;

import com.testing.sunnycommon.annotations.SunnyAdvice;
import com.testing.sunnycommon.model.Result;
import com.testing.sunnycommon.model.mail.SunnyMailConf;
import com.testing.sunnycommon.model.mail.SunnyMailConfBO;
import com.testing.sunnycommon.service.SunnyMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname SunnyMailController
 * @Description 类型说明
 * @Date 2023/6/16 20:29
 * @Created by 特斯汀Roy
 */
@RestController
@RequestMapping("/sunnyMail")
public class SunnyMailController {

    @Autowired
    SunnyMailService sunnyMailService;

    @PostMapping("/sendTest")
    public Result sendTestMail(@RequestBody SunnyMailConf sunnyMailConf) {
        return sunnyMailService.sendTestMail(sunnyMailConf);
    }

    /**
     * 将resultId在url中携带请求
     * @param id
     * @param sunnyMailConf
     * @return
     */
    @PostMapping("/sendReport/{id}")
    public Result sendReportMail(@PathVariable Long id, @RequestBody SunnyMailConf sunnyMailConf) {
        sunnyMailService.sendReportMail(sunnyMailConf, id);
        addSunnyMailConf(sunnyMailConf);
        return Result.success("正在发送邮件报告", "正在发送邮件报告");
    }

    /**
     * 将resultId带在请求体中完成请求
     * @param sunnyMailConfBO
     * @return
     */
    @PostMapping("/sendReportMail")
    public Result sendReportMail(@RequestBody SunnyMailConfBO sunnyMailConfBO) {
        sunnyMailService.sendReportMail(sunnyMailConfBO, sunnyMailConfBO.getResultId());
        return Result.success("正在发送邮件报告", "正在发送邮件报告");
    }

    /**
     * 对于邮件配置的持久化实现
     */

    @GetMapping
    public Result<List<SunnyMailConf>> listSunnyMailConf() {
        return Result.success("查询邮件配置列表成功", sunnyMailService.list());
    }

    @SunnyAdvice("新增邮件配置")
    @PostMapping
    public Result addSunnyMailConf(@RequestBody SunnyMailConf sunnyMailConf) {
        boolean save = sunnyMailService.save(sunnyMailConf);
        if (save) {
            return Result.success("新增邮件配置【" + sunnyMailConf.getName() + "】成功", sunnyMailConf);
        } else {
            return Result.fail(1020, "新增邮件配置【" + sunnyMailConf.getName() + "】失败", sunnyMailConf);
        }
    }

    @SunnyAdvice("修改邮件配置")
    @PutMapping
    public Result updateSunnyMailConf(@RequestBody SunnyMailConf sunnyMailConf) {
        boolean update = sunnyMailService.updateById(sunnyMailConf);

        if (update) {
            return Result.success("修改邮件配置【" + sunnyMailConf.getName() + "】成功", sunnyMailConf);
        } else {
            return Result.fail(1030, "修改邮件配置【" + sunnyMailConf.getName() + "】失败", sunnyMailConf);
        }
    }

    @SunnyAdvice("删除邮件配置")
    @DeleteMapping("/{id}")
    public Result deleteSunnyMailConf(@PathVariable Long id) {
        SunnyMailConf byId = sunnyMailService.getById(id);
        if (byId != null) {
            boolean remove = sunnyMailService.removeById(id);
            if (remove) {
                return Result.success("删除邮件配置【" + byId.getName() + "】成功", byId);
            } else {
                return Result.fail(1040, "删除邮件配置【" + byId.getName() + "】失败", byId);
            }
        } else {
            return Result.fail(1041, "不存在要删除的配置项，请检查请求过程", null);
        }
    }


}
